# config.py - 数据库配置
import os
from urllib.parse import quote_plus

# 数据库配置
class DatabaseConfig:
    # MySQL配置
    MYSQL_HOST = os.getenv('MYSQL_HOST', 'localhost')
    MYSQL_PORT = os.getenv('MYSQL_PORT', '3306')
    MYSQL_USER = os.getenv('MYSQL_USER', 'root')
    MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', 'cyxhbx132')
    MYSQL_DATABASE = os.getenv('MYSQL_DATABASE', 'interview_agent')
    MYSQL_CHARSET = os.getenv('MYSQL_CHARSET', 'utf8mb4')
    
    # 构建数据库URL
    @classmethod
    def get_database_url(cls):
        # URL编码密码以处理特殊字符
        password = quote_plus(cls.MYSQL_PASSWORD)
        return f"mysql+pymysql://{cls.MYSQL_USER}:{password}@{cls.MYSQL_HOST}:{cls.MYSQL_PORT}/{cls.MYSQL_DATABASE}?charset={cls.MYSQL_CHARSET}"
    
    # SQLAlchemy配置
    SQLALCHEMY_ECHO = os.getenv('SQLALCHEMY_ECHO', 'False').lower() == 'true'
    SQLALCHEMY_POOL_SIZE = int(os.getenv('SQLALCHEMY_POOL_SIZE', '10'))
    SQLALCHEMY_POOL_TIMEOUT = int(os.getenv('SQLALCHEMY_POOL_TIMEOUT', '30'))
    SQLALCHEMY_POOL_RECYCLE = int(os.getenv('SQLALCHEMY_POOL_RECYCLE', '3600'))
    SQLALCHEMY_MAX_OVERFLOW = int(os.getenv('SQLALCHEMY_MAX_OVERFLOW', '20'))

# 创建配置实例
_config = DatabaseConfig()
SQLALCHEMY_DATABASE_URL = _config.get_database_url()

# 环境变量设置示例（可以创建.env文件）
"""
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=interview_agent
MYSQL_CHARSET=utf8mb4
SQLALCHEMY_ECHO=False
SQLALCHEMY_POOL_SIZE=10
SQLALCHEMY_POOL_TIMEOUT=30
SQLALCHEMY_POOL_RECYCLE=3600
SQLALCHEMY_MAX_OVERFLOW=20
"""

# 用于测试的配置
class TestDatabaseConfig(DatabaseConfig):
    MYSQL_DATABASE = 'interview_agent_test'
    SQLALCHEMY_ECHO = True

# 根据环境选择配置
def get_config():
    env = os.getenv('FLASK_ENV', 'development')
    if env == 'testing':
        return TestDatabaseConfig()
    return DatabaseConfig()

# 默认配置
config = get_config()

